<?php

/**
 * Created by PhpStorm.
 * User: bob
 * Date: 21/9/25
 * Time: 上午10:37
 */

namespace App\Api\Web\Controllers;

use App\Api\Web\Models\Bbs;
use App\Api\Web\Services\BbsService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BbsController extends Controller
{
    public function bbsRelease(Request $request)
    {
        $params = $request->all();
        $user = authUser();
        $r = BbsService::release($params, 'web', $user->id);
        return $this->formatOutput($r);
    }

    public function bbsIndex(Request $request)
    {
        $params = $request->all();
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);


        $r = Bbs::where('bbs.status', ">=", 200)
            ->leftJoin('users as u', 'u.id', '=', 'bbs.user_id')
            ->select('u.name', 'bbs.*', 'u.head_img')
            ->where('bbs.status', '>', '-900')
            ->orderBy('bbs.id', 'desc')
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        foreach ($r['data'] as $k => $v) {
            $r['data'][$k]['status_name'] = data_get(Bbs::$status, data_get($v, 'status', ''), '未知');
        }

        return $this->formatOutput($r);
    }

    public function bbsMine(Request $request)
    {
        $params = $request->all();

        $user = authUser();

        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);

        $r = Bbs::select('u.name', 'bbs.*')
            ->leftJoin('users as u', 'u.id', '=', 'bbs.user_id')
            ->where('bbs.user_id', '=', $user->id)
            ->where('bbs.status', '>', '-900')
            ->orderBy('bbs.id', 'desc')
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        foreach ($r['data'] as $k => $v) {
            $r['data'][$k]['status_name'] = data_get(Bbs::$status, data_get($v, 'status', ''), '未知');
        }

        return $this->formatOutput($r);
    }

    public function bbsOthers(Request $request)
    {
        $params = $request->all();

        $userId = data_get($params, 'user_id', '');

        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);

        $r = Bbs::select('u.name', 'bbs.*')
            ->leftJoin('users as u', 'u.id', '=', 'bbs.user_id')
            ->where('bbs.user_id', '=', $userId)
            ->where('bbs.status', '>', '-900')
            ->orderBy('bbs.id', 'desc')
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        foreach ($r['data'] as $k => $v) {
            $r['data'][$k]['status_name'] = data_get(Bbs::$status, data_get($v, 'status', ''), '未知');
        }

        return $this->formatOutput($r);
    }

    public function bbsDelete(Request $request)
    {
        $params = $request->all();
        $user = authUser();
        $id = data_get($params, 'id', '');
        if (empty($id)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        Bbs::where('id', '=', $id)
            ->where('user_id', '=', $user->id)
            ->update([
                'status' => '-900'
            ]);

        return $this->formatOutput('success');
    }
}
